[newbie] sql : simple select question

Hi, My SQL lessons are very far :(

CREATE TABLE CONTACTS
(ID INT,
NAME VARCHAR(10)
PHONE VARCHAR(10))

CREATE TABLE CLASS
(CONTACT INT,
CAT INT)

INSERT INTO CONTACTS VALUES (101, 'toto', '3545')
INSERT INTO CONTACTS VALUES (102, 'pif', '8421')
INSERT INTO CONTACTS VALUES (103, 'loulou', '1245')

INSERT INTO CLASS VALUES (101, 1)
INSERT INTO CLASS VALUES (101, 2)
INSERT INTO CLASS VALUES (102, 1)

toto owns to category 1 and 2
pif owns to category 1
loulou owns to none categories

=> how can I display contacts who own to none categories (like loulou) ?

I tried:
SELECT * FROM contacts LEFT OUTER JOIN class ON id = contact
I get the list of all contacts...
but if I add 'WHERE cat = NULL' : there's no result :(

Thanx for your help !
tlouaze [ Do, 28 September 2006 14:20 ] [ ID #1482191 ]

Re: [newbie] sql : simple select question

"Tony Louazé" <tlouaze [at] noos.fr> wrote in message
news:451bbcfc$0$17446$79c14f64 [at] nan-newsreader-07.noos.net...
> Hi, My SQL lessons are very far :(
>
> CREATE TABLE CONTACTS
> (ID INT,
> NAME VARCHAR(10)
> PHONE VARCHAR(10))
>
> CREATE TABLE CLASS
> (CONTACT INT,
> CAT INT)
>
> INSERT INTO CONTACTS VALUES (101, 'toto', '3545')
> INSERT INTO CONTACTS VALUES (102, 'pif', '8421')
> INSERT INTO CONTACTS VALUES (103, 'loulou', '1245')
>
> INSERT INTO CLASS VALUES (101, 1)
> INSERT INTO CLASS VALUES (101, 2)
> INSERT INTO CLASS VALUES (102, 1)
>
> toto owns to category 1 and 2
> pif owns to category 1
> loulou owns to none categories
>
> => how can I display contacts who own to none categories (like loulou) ?
>
> I tried:
> SELECT * FROM contacts LEFT OUTER JOIN class ON id = contact
> I get the list of all contacts...
> but if I add 'WHERE cat = NULL' : there's no result :(
>
> Thanx for your help !
>
>

isn't that what you'd expect? there don't seem to be any null cats in what
you show....
Johnny [ Do, 28 September 2006 19:26 ] [ ID #1482192 ]

Re: [newbie] sql : simple select question

"Johnny" <removethis.huuanito [at] hotmail.com> wrote in message
news:MATSg.464$UJ2.159 [at] fed1read07...
>
> "Tony Louazé" <tlouaze [at] noos.fr> wrote in message
> news:451bbcfc$0$17446$79c14f64 [at] nan-newsreader-07.noos.net...
> > Hi, My SQL lessons are very far :(
> >
> > CREATE TABLE CONTACTS
> > (ID INT,
> > NAME VARCHAR(10)
> > PHONE VARCHAR(10))
> >
> > CREATE TABLE CLASS
> > (CONTACT INT,
> > CAT INT)
> >
> > INSERT INTO CONTACTS VALUES (101, 'toto', '3545')
> > INSERT INTO CONTACTS VALUES (102, 'pif', '8421')
> > INSERT INTO CONTACTS VALUES (103, 'loulou', '1245')
> >
> > INSERT INTO CLASS VALUES (101, 1)
> > INSERT INTO CLASS VALUES (101, 2)
> > INSERT INTO CLASS VALUES (102, 1)
> >
> > toto owns to category 1 and 2
> > pif owns to category 1
> > loulou owns to none categories
> >
> > => how can I display contacts who own to none categories (like loulou) ?
> >
> > I tried:
> > SELECT * FROM contacts LEFT OUTER JOIN class ON id = contact
> > I get the list of all contacts...
> > but if I add 'WHERE cat = NULL' : there's no result :(
> >
> > Thanx for your help !
> >
> >
>
> isn't that what you'd expect? there don't seem to be any null cats in what
> you show....
>
>

oops! not enough coffee yet! wait one :-)
Johnny [ Do, 28 September 2006 19:47 ] [ ID #1482193 ]

Re: [newbie] sql : simple select question

"Johnny" <removethis.huuanito [at] hotmail.com> wrote in message
news:YTTSg.465$UJ2.42 [at] fed1read07...
>
> "Johnny" <removethis.huuanito [at] hotmail.com> wrote in message
> news:MATSg.464$UJ2.159 [at] fed1read07...
> >
> > "Tony Louazé" <tlouaze [at] noos.fr> wrote in message
> > news:451bbcfc$0$17446$79c14f64 [at] nan-newsreader-07.noos.net...
> > > Hi, My SQL lessons are very far :(
> > >
> > > CREATE TABLE CONTACTS
> > > (ID INT,
> > > NAME VARCHAR(10)
> > > PHONE VARCHAR(10))
> > >
> > > CREATE TABLE CLASS
> > > (CONTACT INT,
> > > CAT INT)
> > >
> > > INSERT INTO CONTACTS VALUES (101, 'toto', '3545')
> > > INSERT INTO CONTACTS VALUES (102, 'pif', '8421')
> > > INSERT INTO CONTACTS VALUES (103, 'loulou', '1245')
> > >
> > > INSERT INTO CLASS VALUES (101, 1)
> > > INSERT INTO CLASS VALUES (101, 2)
> > > INSERT INTO CLASS VALUES (102, 1)
> > >
> > > toto owns to category 1 and 2
> > > pif owns to category 1
> > > loulou owns to none categories
> > >
> > > => how can I display contacts who own to none categories (like loulou)
?
> > >
> > > I tried:
> > > SELECT * FROM contacts LEFT OUTER JOIN class ON id = contact
> > > I get the list of all contacts...
> > > but if I add 'WHERE cat = NULL' : there's no result :(
> > >
> > > Thanx for your help !
> > >
> > >
> >
> > isn't that what you'd expect? there don't seem to be any null cats in
what
> > you show....
> >
> >
>
> oops! not enough coffee yet! wait one :-)
>

try
IS NULL
in place of
= NULL

testing for null is a special case
Johnny [ Do, 28 September 2006 20:24 ] [ ID #1482194 ]

Re: [newbie] sql : simple select question

thanx it works !
I thought and I think there's surely another way to get the same result
(without testing a NULL value)

>> > "Tony Louazé" wrote :
>> > > Hi, My SQL lessons are very far :(
>> > >
>> > > CREATE TABLE CONTACTS
>> > > (ID INT,
>> > > NAME VARCHAR(10)
>> > > PHONE VARCHAR(10))
>> > >
>> > > CREATE TABLE CLASS
>> > > (CONTACT INT,
>> > > CAT INT)
>> > >
>> > > INSERT INTO CONTACTS VALUES (101, 'toto', '3545')
>> > > INSERT INTO CONTACTS VALUES (102, 'pif', '8421')
>> > > INSERT INTO CONTACTS VALUES (103, 'loulou', '1245')
>> > >
>> > > INSERT INTO CLASS VALUES (101, 1)
>> > > INSERT INTO CLASS VALUES (101, 2)
>> > > INSERT INTO CLASS VALUES (102, 1)
>> > >
>> > > toto owns to category 1 and 2
>> > > pif owns to category 1
>> > > loulou owns to none categories
>> > >
>> > > => how can I display contacts who own to none categories (like
>> > > loulou)
> ?
>> > >
>> > > I tried:
>> > > SELECT * FROM contacts LEFT OUTER JOIN class ON id = contact
>> > > I get the list of all contacts...
>> > > but if I add 'WHERE cat = NULL' : there's no result :(
>> > >
>> > > Thanx for your help !
>> > >
>> > >
>> >
>> > isn't that what you'd expect? there don't seem to be any null cats in
> what
>> > you show....
>> >
>> >
>>
>> oops! not enough coffee yet! wait one :-)
>>
>
> try
> IS NULL
> in place of
> = NULL
>
> testing for null is a special case
>
>
tlouaze [ Fr, 29 September 2006 00:26 ] [ ID #1482195 ]
PHP » alt.php.sql » [newbie] sql : simple select question

Vorheriges Thema: MySQL 5 -- "Invalid default value"?
Nächstes Thema: Dates not coming back in order....